#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author:    御风
# @Mail:      zhong.yufeng@foxmail.com
# @Time:      2022/4/9 1:52


import io
import json


class ApiResult(object):

    @staticmethod
    def json(code, msg, data=None):
        ret = json.dumps({
            "code": code,
            "msg":  msg,
            "data": data
        }, ensure_ascii=False, separators=(", ", ": "))
        return ret, 200, { "Content-Type": "application/json" }

    @staticmethod
    def success(data=None, msg="success"):
        return ApiResult.json(0, msg, data)

    @staticmethod
    def error(code=-1, msg="error", data=None):
        return ApiResult.json(code, msg, data)

    @staticmethod
    def image(img):
        imgio = io.BytesIO()
        img.save(imgio, "png")
        imgdata = imgio.getvalue()
        return imgdata, 200, { "Content-Type": "image/png" }


if __name__ == "__main__":
    print(ApiResult.json(0, "success", "test1"))
    print(ApiResult.success(data="test2"))
    print(ApiResult.error(data="test3"))
